// 成就系统
let achievements = {
    combo10: {
        id: 'combo10',
        title: '连击达人',
        description: '达成10连击',
        icon: '🎯',
        unlocked: false,
        check: () => combo >= 10
    },
    score500: {
        id: 'score500',
        title: '高分王者',
        description: '单局得分超过500分',
        icon: '👑',
        unlocked: false,
        check: () => score >= 500
    },
    perfect: {
        id: 'perfect',
        title: '完美游戏',
        description: '在困难模式下完成游戏且未失误',
        icon: '🌟',
        unlocked: false,
        check: () => gameMode === 'hard' && missCount === 0 && timeLeft === 0
    },
    speedKing: {
        id: 'speedKing',
        title: '闪电手',
        description: '1秒内连续击中3只地鼠',
        icon: '⚡',
        unlocked: false,
        check: () => combo >= 3 && Date.now() - lastHitTime < 1000
    }
};

// 创建成就提示框
function createAchievementToast() {
    const toastContainer = document.createElement('div');
    toastContainer.id = 'achievementToasts';
    toastContainer.style.cssText = `
        position: fixed;
        top: 20px;
        right: 20px;
        z-index: 1000;
    `;
    document.body.appendChild(toastContainer);
}

// 显示成就解锁提示
function showAchievementUnlock(achievement) {
    const toast = document.createElement('div');
    toast.className = 'achievement-toast';
    toast.style.cssText = `
        background: rgba(0, 0, 0, 0.8);
        color: white;
        padding: 15px;
        border-radius: 10px;
        margin-bottom: 10px;
        display: flex;
        align-items: center;
        animation: slideIn 0.5s ease-out;
        backdrop-filter: blur(5px);
        box-shadow: 0 4px 15px rgba(0, 0, 0, 0.2);
    `;

    toast.innerHTML = `
        <div style="font-size: 2em; margin-right: 10px;">${achievement.icon}</div>
        <div>
            <div style="font-weight: bold;">${achievement.title}</div>
            <div style="font-size: 0.8em;">${achievement.description}</div>
        </div>
    `;

    const toastContainer = document.getElementById('achievementToasts');
    toastContainer.appendChild(toast);

    // 3秒后淡出消失
    setTimeout(() => {
        toast.style.animation = 'fadeOut 0.5s ease-out';
        setTimeout(() => toast.remove(), 500);
    }, 3000);
}

// 检查成就
function checkAchievements() {
    Object.values(achievements).forEach(achievement => {
        if (!achievement.unlocked && achievement.check()) {
            achievement.unlocked = true;
            showAchievementUnlock(achievement);
            // 保存成就状态到本地存储
            localStorage.setItem('achievements', JSON.stringify(achievements));
        }
    });
}

// 获取成就列表
function getAchievementsList() {
    return Object.values(achievements).map(achievement => ({
        id: achievement.id,
        title: achievement.title,
        description: achievement.description,
        icon: achievement.icon,
        unlocked: achievement.unlocked || false
    }));
}

// 添加CSS动画
const style = document.createElement('style');
style.textContent = `
    @keyframes slideIn {
        from { transform: translateX(100%); opacity: 0; }
        to { transform: translateX(0); opacity: 1; }
    }

    @keyframes fadeOut {
        from { transform: translateX(0); opacity: 1; }
        to { transform: translateX(100%); opacity: 0; }
    }
`;
document.head.appendChild(style);

// 初始化成就系统
function initAchievements() {
    createAchievementToast();
    // 从本地存储加载成就状态
    const savedAchievements = JSON.parse(localStorage.getItem('achievements') || '{}');
    // 更新成就状态
    Object.keys(achievements).forEach(key => {
        if (savedAchievements[key]) {
            achievements[key].unlocked = savedAchievements[key].unlocked;
        }
    });
}

document.addEventListener('DOMContentLoaded', initAchievements);